// pages/group/group.js
var g = require("../../utils/group");
Page({
    kaishi(){
        var database = wx.cloud.database()
        var k = this.data.index
        var matchid = this.data.result[k]._id
        database.collection("match").doc(matchid).update({
            data:{
                beginTime:new Date()
            }
        }).then((res)=>{
            if(res){
                wx.showToast({
                    title: '开始比赛成功',
                })
            }
        }).catch((err)=>{
            console.log("异常捕获"+err)
        })
    },

  join() {
    var index = this.data.index;
    var matchid = this.data.result[index]._id;
    var title = this.data.result[index].matchname;
    var userid = this.data.userid;
    var username = this.data.username;
    var database = wx.cloud.database();
    database.collection("baoming").add({
      data: {
        matchid: matchid,
        title: title,
        userid: userid,
        username: username
      }
    }).then((res) => {
      if (res) {
        wx.showToast({
          title: '报名成功',
        })
      } else {
        wx.showToast({
          title: '报名失败',
        })
      }
    }).catch((err) => {
      console.log("异常捕获：" + err);
    })
  },

  matchname: function (e) {
    this.setData({
      matchname: e.detail.value
    })
  },

  create() {
    var that = this;
    var database = wx.cloud.database();
    database.collection("match").add({
      data: {
        matchname: that.data.matchname,
        userid: that.data.userid,
        imageList: that.data.imageList
      }
    }).then((res) => {
      if (res) {
        wx.showToast({
          title: '数据插入成功',
        })
      } else {
        wx.showToast({
          title: '数据插入失败',
        })
      }
    })
  },

  picname: function (e) {
    this.setData({
      picname: e.detail.value
    })
  },

  upload() {
    var that = this
    wx.chooseMedia({
      count: 1,
      success(res) {
        console.log(res.tempFiles)
        var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
        var ext = res.tempFiles[0].tempFilePath.slice(po)
        wx.cloud.uploadFile({
          cloudPath: that.data.picname + ext,
          filePath: res.tempFiles[0].tempFilePath,
          success(res) {
            console.log(res.fileID)
            that.data.imageList.push(res.fileID);
            that.setData({
              imageList: that.data.imageList
            });
          }
        })
      }
    })
  },

  delete() {
    var k = this.data.deleteindex;
    this.data.imageList.splice(k, 1);
    this.setData({
      imageList: this.data.imageList,
      flag3: false
    })
  },

  choose: function (e) {
    var k = e.currentTarget.id;
    var loc = this.data.imageList[k];
    this.setData({
      currentImage: loc,
      flag3: true,
      deleteindex: k
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    isKaishi: false,
    username: null,
    groupname: null,
    groupnum: null,
    xuanshou: [],
    matches: [],
    deleteindex: 0,
    currentImage: null,
    imageList: [],
    picname: null,
    userid: null,
    titles: g.list,
    category: 0,
    matchName: null,
    result: [],
    index: 0,
    flag: false,
    flag2: false,
    flag3: false
  },
  changeCategory: function (e) {
    this.setData({
      category: e.currentTarget.id
    });
  },

  setMathName: function (e) {
    this.setData({
      matchName: e.detail.value
    })
  },
  setGroupName: function (e) {
    this.setData({
      groupname: e.detail.value
    })
  },
  setGroupNumber: function (e) {
    this.setData({
      groupnum: e.detail.value
    })
  },

  searchgroup(){
        var that = this
        var groupname = this.data.groupname
        var database = wx.cloud.database()
        console.log(groupname)
        database.collection("group").where({
            groupname:groupname
        }).get().then((res)=>{
            that.setData({
                result:res.data
            })
        })
  },

  tapback: function (e) {
    this.setData({
      index: e.currentTarget.id,
      flag: true,
      flag2: false
    });
  },

  updateResultList() {
    var result = [];
    const matches = this.data.matches;
    const matchName = this.data.matchName;
    const length = matches.length;
    for (let i = 0; i < length; i++) {
      if (matchName == null || matchName == matches[i].title) {
        result.push(matches[i]);
      }
    }
    this.setData({
      result: result
    });
  },

  search() {
    wx.cloud.database()
            .collection("match")
            .get()
            .then((res) => {
              this.data.matches = res.data;
              console.log(this.data.matches);
              this.updateResultList();
            })
            .catch((err) => {
              console.log(err);
            });
  },

  tapback2: function (e) {
    var database = wx.cloud.database();
    var k = e.currentTarget.id;
    var data = this.data.result[k];
    var that = this;

    this.setData({
        isKaishi:false
    })

    //判断当前用户是否为比赛的创建者
    if(data.userid == this.data.userid){
        this.setData({
            isKaishi:true
        })
    }

    database.collection("baoming").where({
      matchid: data._id
    }).get().then((res) => {
      that.setData({
        xuanshou: res.data
      })
    })
    

    this.setData({
      index: e.currentTarget.id,
      flag2: true,
      flag: false
    });
  },

  jiaruxiaozu(){
    var k = this.data.index
    var xiaozu = this.data.result[k].zuyuan
    var groupid = this.data.result[k]._id
    xiaozu.push(this.data.username)
    var database = wx.cloud.database()
    database.collection("group").doc(groupid).update({
        data:{
            zuyuan:xiaozu
        }
    }).then((res)=>{
        if(res){
            wx.showToast({
                title: '加入小组成功',
            })
        }else{
            wx.showToast({
                title: '加入小组失败',
            })
        }
    })
  },
  creategroup(){
    var that = this
    var database = wx.cloud.database()
    database.collection("group").add({
        data:{
            groupname:that.data.groupname,
            groupnum:that.data.groupnum,
            zuyuan:[],
            userid:that.data.userid,
            username:that.data.username
        }
    }).then((res)=>{
        if(res){
            wx.showToast({
                title: '小组创建成功',
            })
        }
    })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    wx.getStorage({
        key: "user",
        success(res) {
            that.setData({
                userid: res.data.id
            })
            wx.cloud.database()
                    .collection("user")
                    .where({
                        _id: res.data.id
                    })
                    .get()
                    .then((msg)=>{
                        that.setData({
                            username:msg.data[0].name
                        });
                    }).catch((err)=>{
                        console.log(err);
                    })
        }
    })
  }
})